package com.breakfast.fun.ui.user.address;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;

import com.breakfast.fun.R;
import com.breakfast.fun.adapter.user.UserAddressAdapter;
import com.breakfast.fun.base.BaseActivity;
import com.breakfast.fun.data.entity.user.address.Address;
import com.breakfast.fun.data.entity.user.address.AddressData;
import com.breakfast.fun.event.FinishEvent;
import com.breakfast.fun.event.RefreshEvent;
import com.breakfast.fun.util.L;
import com.breakfast.fun.view.widget.TitleBarView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import butterknife.Bind;

public class UserAddressActivity extends BaseActivity<UserAddressPresenter, UserAddressModel> implements UserAddressContract.View {


    @Bind(R.id.tbv_user_address)
    TitleBarView tbvUserAddress;
    @Bind(R.id.iv_address_position)
    ImageView ivAddressPosition;
    @Bind(R.id.rl_add_address)
    RelativeLayout rlAddAddress;
    @Bind(R.id.lv_address)
    ListView lvAddress;
    @Bind(R.id.iv_address_empty)
    ImageView ivAddressEmpty;

    private UserAddressAdapter adapter;
    private AddressData curAddress;

    @Override
    public int getLayoutId() {
        return R.layout.activity_user_address;
    }

    @Override
    public void initView() {
        EventBus.getDefault().register(this);
        tbvUserAddress.setTitleNoRight("送餐地址");
        tbvUserAddress.setBack(this);

        rlAddAddress.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(UserAddressActivity.this, AddAddressActivity.class);
                startActivity(intent);
            }
        });


    }

    @Override
    public void showAddress(Address address) {
        Boolean isFromCart = getIntent().getBooleanExtra("isFromCart", false);
        adapter = new UserAddressAdapter(context, address.getData(), isFromCart);
        if ((curAddress = (AddressData) getIntent().getSerializableExtra("chooseAddress")) != null) {
            adapter.setCurAddress(curAddress);
        }
        lvAddress.setAdapter(adapter);

        if (address.getData().size() == 0) {
            ivAddressEmpty.setVisibility(View.VISIBLE);
        } else {
            ivAddressEmpty.setVisibility(View.GONE);
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventRefresh(RefreshEvent refreshEvent) {
        if (refreshEvent.getActivityName().equals(getClass().getSimpleName())) {
            presenter.getAddress();
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onEventFinish(FinishEvent finishEvent) {
        if (finishEvent.getActivityName().equals(getClass().getSimpleName())) {
            UserAddressActivity.this.finish();
        }
    }

    @Override
    protected void destroyView() {
        L.d("destroy");
        super.destroyView();
        EventBus.getDefault().unregister(this);
        if (adapter != null) {
            adapter.setCurAddress(null);
        }
    }
}
